package com.gsma.calendar.android.widget;

import java.util.Date;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

/**
 * Custom view that draws a vertical time "ruler" representing the chronological
 * progression of a single day. Usually shown along with {@link SessionView}
 * instances to give a spatial sense of time.
 */
public class TimeRulerView extends View {

    private int mHeaderWidth = 50;
    private int mHourHeight = 90;
    private int mLabelTextSize = 20;
    private int mLabelPaddingLeft = 8;
    private int mLabelColor = Color.BLACK;
    private int mDividerColor = Color.LTGRAY;
    private int mStartHour = 9;
    private int mEndHour = 19;

    public TimeRulerView(Context context) {
        this(context, null);
    }

    public TimeRulerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TimeRulerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        //final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TimeRulerView, defStyle, 0);
        
        final float density = getResources().getDisplayMetrics().density;
        mHeaderWidth = Math.round(46 * density);
        mHourHeight = Math.round(50 * density); 
        mLabelTextSize = Math.round(14 * density);

        //a.recycle();
    }

    /**
     * Return the vertical offset (in pixels) for a requested time (in
     * milliseconds since epoch).
     */
    public int getTimeVerticalOffset(long timeMillis) {
       	Date d = new Date(timeMillis);
		final int minutes = ((d.getHours() - mStartHour) * 60) + d.getMinutes();
        return (minutes * mHourHeight * 2) / 60;
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int hours = mEndHour - mStartHour;

        int width = mHeaderWidth;
        int height = mHourHeight * hours * 2;

        setMeasuredDimension(resolveSize(width, widthMeasureSpec),
                resolveSize(height, heightMeasureSpec));
    }

    private Paint mDividerPaint = new Paint();
    private Paint mLabelPaint = new Paint();

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        final int hourHeight = mHourHeight;

        final Paint dividerPaint = mDividerPaint;
        dividerPaint.setColor(mDividerColor);
        dividerPaint.setStyle(Style.FILL);

        final Paint labelPaint = mLabelPaint;
        labelPaint.setColor(mLabelColor);
        labelPaint.setTextSize(mLabelTextSize);
        //labelPaint.setTypeface(Typeface.DEFAULT_BOLD);
        labelPaint.setAntiAlias(true);

        final FontMetricsInt metrics = labelPaint.getFontMetricsInt();
        final int labelHeight = Math.abs(metrics.ascent);
        final int labelOffset = labelHeight + mLabelPaddingLeft;

        final int right = getRight();

        // Walk left side of canvas drawing timestamps
        final int hours = mEndHour - mStartHour;
        int dividerY = 0;
        for (int i = 0; i < hours; i++) {
        	final int hour = mStartHour + i; // :00
            
        	canvas.drawLine(0, dividerY, right, dividerY, dividerPaint);
            canvas.drawRect(0, dividerY, mHeaderWidth, dividerY + hourHeight, dividerPaint); // draw text title for timestamp
            String label = hour + ":00";
            float labelWidth = labelPaint.measureText(label);
            canvas.drawText(label, 0, label.length(), mHeaderWidth - labelWidth - mLabelPaddingLeft, dividerY + labelOffset, labelPaint);
            dividerY += hourHeight;
            
            if(hour != mEndHour) { // :30
	            canvas.drawLine(0, dividerY, right, dividerY, dividerPaint);
	            canvas.drawRect(0, dividerY, mHeaderWidth, dividerY + hourHeight, dividerPaint); // draw text title for timestamp
	            label = hour + ":30";
	            labelWidth = labelPaint.measureText(label);
	            canvas.drawText(label, 0, label.length(), mHeaderWidth - labelWidth - mLabelPaddingLeft, dividerY + labelOffset, labelPaint);
	            dividerY += hourHeight;
            }
        }
    }

    public int getHeaderWidth() {
        return mHeaderWidth;
    }
}
